home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / gawk-3.000 / gawk-3 / gawk-3.0.0 / awklib / igawk.save < prev    next >
Encoding:
Text File  |  1996-01-08  |  2.7 KB  |  121 lines

  1. #! /bin/sh
  2.  
  3. # igawk --- like gawk but do @include processing
  4. # Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
  5. # July 1993
  6.  
  7. if [ "$1" = debug ]
  8. then
  9.     set -x
  10.     shift
  11. else
  12.     # cleanup on exit, hangup, interrupt, quit, termination
  13.     trap 'rm -f /tmp/ig.[se].$$' 0 1 2 3 15
  14. fi
  15.  
  16. while [ $# -ne 0 ] # loop over arguments
  17. do
  18.     case $1 in
  19.     --)     shift; break;;
  20.  
  21.     -W)     shift
  22.             set -- -W"$@"
  23.             continue;;
  24.  
  25.     -[vF])  opts="$opts $1 '$2'"
  26.             shift;;
  27.  
  28.     -[vF]*) opts="$opts '$1'" ;;
  29.  
  30.     -f)     echo @include "$2" >> /tmp/ig.s.$$
  31.             shift;;
  32.  
  33.     -f*)    f=`echo "$1" | sed 's/-f//'`
  34.             echo @include "$f" >> /tmp/ig.s.$$ ;;
  35.  
  36.     -?file=*)    # -Wfile or --file
  37.             f=`echo "$1" | sed 's/-.file=//'`
  38.             echo @include "$f" >> /tmp/ig.s.$$ ;;
  39.  
  40.     -?file)    # get arg, $2
  41.             echo @include "$2" >> /tmp/ig.s.$$
  42.             shift;;
  43.  
  44.     -?source=*)    # -Wsource or --source
  45.             t=`echo "$1" | sed 's/-.source=//'`
  46.             echo "$t" >> /tmp/ig.s.$$ ;;
  47.  
  48.     -?source)  # get arg, $2
  49.             echo "$2" >> /tmp/ig.s.$$
  50.             shift;;
  51.  
  52.     --*)    opts="$opts '$1'" ;;
  53.  
  54.     *)      break;;
  55.     esac
  56.  
  57.     shift
  58. done
  59.  
  60. if [ ! -s /tmp/ig.s.$$ ]
  61. then
  62.     echo "$1" > /tmp/ig.s.$$
  63.     shift
  64. fi
  65.  
  66. # at this point, /tmp/ig.s.$$ has the program
  67. gawk -- '
  68. # process @include directives
  69.  
  70. function pathto(file,    i, t, junk)
  71. {
  72.     if (index(file, "/") != 0)
  73.         return file
  74.  
  75.     for (i = 1; i <= ndirs; i++) {
  76.         t = (pathlist[i] "/" file)
  77.         if ((getline junk < t) > 0) {
  78.             # found it
  79.             close(t)
  80.             return t
  81.         }
  82.     }
  83.     return ""
  84. }
  85. BEGIN {
  86.     path = ENVIRON["AWKPATH"]
  87.     ndirs = split(path, pathlist, ":")
  88.     for (i = 1; i <= ndirs; i++) {
  89.         if (pathlist[i] == "")
  90.             pathlist[i] = "."
  91.     }
  92.     stackptr = 0
  93.     input[stackptr] = ARGV[1] # ARGV[1] is first file
  94.  
  95.     for (; stackptr >= 0; stackptr--) {
  96.         while ((getline < input[stackptr]) > 0) {
  97.             if (tolower($1) != "@include") {
  98.                 print
  99.                 continue
  100.             }
  101.             fpath = pathto($2)
  102.             if (fpath == "") {
  103.                 printf("igawk:%s:%d: cannot find %s\n", \
  104.                     input[stackptr], FNR, $2) > "/dev/stderr"
  105.                 continue
  106.             }
  107.             if (! (fpath in processed)) {
  108.                 processed[fpath] = input[stackptr]
  109.                 input[++stackptr] = fpath
  110.             } else
  111.                 print $2, "included in", input[stackptr], \
  112.                     "already included in", \
  113.                     processed[fpath] > "/dev/stderr"
  114.         }
  115.         close(input[stackptr])
  116.     }
  117. }' /tmp/ig.s.$$ > /tmp/ig.e.$$
  118. eval gawk -f /tmp/ig.e.$$ $opts -- "$@"
  119.  
  120. exit $?
  121.